import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutterclue/mvp/page/ClueStatisticsPage.dart';
import 'MessagePage.dart';
import 'ClueManagerPage.dart';
import 'AddCluePage.dart';
import 'DrawerPage.dart';
import 'ClueDetailPage.dart';
import 'package:flutterclue/view/ClueManagerSelectPop.dart';
import 'package:flutterclue/utils/JpushUtil.dart';
import 'package:flutterclue/utils/SharePreferensUtil.dart';

class MainCluePage extends StatefulWidget {
  @override
  _MainCluePageState createState() => new _MainCluePageState();
}

class _MainCluePageState extends State<MainCluePage> {
  List<BottomNavigationBarItem> _navigationViews;
  int _tabIndex = 0;
  ClueSelectMode _selectClueSelectMode;

  var appBarTitle = ['消息', '新增线索', '线索管理', "统计"];

  var _body;

  _initPage() {
    _body = new IndexedStack(
      index: _tabIndex,
      children: <Widget>[
        new MessagePage(),
        new AddCluePage(),
        new ClueManagerPage(
            timeType: _selectClueSelectMode == null
                ? 0
                : _selectClueSelectMode.selectType),
        new ClueStatisticsPage()
      ],
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _navigationViews = <BottomNavigationBarItem>[
      new BottomNavigationBarItem(
          icon: Image.asset("images/tab_msg_off.png", height: 25, width: 25),
          activeIcon:
              Image.asset("images/tab_msg_on.png", height: 25, width: 25),
          title: new Text(appBarTitle[0]),
          backgroundColor: new Color(0xff0188fb)),
      new BottomNavigationBarItem(
        icon: Image.asset("images/tab_add_clue_normal.png",
            height: 25, width: 25),
        activeIcon: Image.asset("images/tab_add_clue_selected.png",
            height: 25, width: 25),
        title: new Text(appBarTitle[1]),
      ),
      new BottomNavigationBarItem(
          icon: Image.asset("images/tab_clue_off.png", height: 25, width: 25),
          activeIcon:
              Image.asset("images/tab_clue_on.png", height: 25, width: 25),
          title: new Text(appBarTitle[2]),
          backgroundColor: new Color(0xff0188fb)),
      new BottomNavigationBarItem(
        icon: Image.asset("images/tab_person_off.png", height: 25, width: 25),
        activeIcon:
            Image.asset("images/tab_person_on.png", height: 25, width: 25),
        title: new Text(appBarTitle[1]),
      ),
    ];
    getJpush();
  }

  void getJpush() async {
    String clueId = await SharePreferencesUtil.getString("clueIdJ");
    String messageType = await SharePreferencesUtil.getString("messageTypeJ");
    print(clueId + "-------" + messageType);
    if (clueId.isNotEmpty && int.parse(messageType) != 0) {
      Navigator.of(context).push(new PageRouteBuilder(
          opaque: false,
          pageBuilder: (BuildContext context, _, __) {
            return new ClueDetailPage(clueId: int.tryParse(clueId));
          },
          transitionsBuilder:
              (_, Animation<double> animation, __, Widget child) {
            return new SlideTransition(
              //相当于android中的估值器
              position: new Tween<Offset>(
                begin: const Offset(1.0, 0.0),
                end: const Offset(0.0, 0.0),
                //相当于android中的差值器
              ).animate(new CurvedAnimation(
                  parent: animation, curve: Curves.easeOut)),
              child: child,
            );
          }));
    }
  }

  void _requestPop() {
    showDialog<Null>(
        context: context,
        builder: (BuildContext context) =>
            new AlertDialog(content: new Text('退出app'), actions: <Widget>[
              new FlatButton(
                  onPressed: () {
                    Navigator.pop(context);
                    SystemNavigator.pop();
                  },
                  child: new Text('确定'))
            ]));
  }

  @override
  Widget build(BuildContext context) {
    _initPage();
    // TODO: implement build
    return new WillPopScope(
      child: new Scaffold(
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(52),
          child: new AppBar(
            elevation: 0.0,
            centerTitle: true,
            title: Stack(
              alignment: AlignmentDirectional.center,
              children: <Widget>[
                new Offstage(
                  offstage: _tabIndex == 2 ? true : false,
                  child: new Text(
                    appBarTitle[_tabIndex],
                    style: new TextStyle(color: Colors.white, fontSize: 18),
                  ),
                ),
                new Offstage(
                    offstage: _tabIndex == 2 ? false : true,
                    child: Container(
                      height: 52,
                      child: new ClueManagerButton(
                        buttonName: _selectClueSelectMode == null
                            ? "全部"
                            : _selectClueSelectMode.showString,
                        selectType: _selectClueSelectMode == null
                            ? 0
                            : _selectClueSelectMode.selectType,
                        onSelect: (value) {
                          _selectClueSelectMode = value;
                          setState(() {});
                        },
                      ),
                    ))
              ],
            ),
            actions: <Widget>[
              new IconButton(
                  icon: new Icon(Icons.search),
                  color: Colors.white,
                  onPressed: () {
                    Navigator.of(context).push(new PageRouteBuilder(
                        opaque: false,
                        pageBuilder: (BuildContext context, _, __) {
                          return new ClueDetailPage();
                        },
                        transitionsBuilder:
                            (_, Animation<double> animation, __, Widget child) {
                          return new SlideTransition(
                            //相当于android中的估值器
                            position: new Tween<Offset>(
                              begin: const Offset(1.0, 0.0),
                              end: const Offset(0.0, 0.0),
                              //相当于android中的差值器
                            ).animate(new CurvedAnimation(
                                parent: animation, curve: Curves.easeOut)),
                            child: child,
                          );
                        }));
                  }),
            ],
          ),
        ),
        drawer: new DrawerPage(),
        body: _body,
        bottomNavigationBar: new BottomNavigationBar(
          items: _navigationViews,
          currentIndex: _tabIndex,
          type: BottomNavigationBarType.fixed,
          onTap: (index) {
            setState(() {
              _tabIndex = index;
            });
          },
        ),
      ),
      onWillPop: () {
        _requestPop();
      },
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    JpushUtil.dispose();
  }
}
